using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Configuration;
using Utility.Logging;
using Utility;

namespace Tort.Database.Providers
{
    internal class DbDetails
    {
        internal DbDetails(string connString, string provider)
        {
            Build(connString, provider);
        }

        internal DbConnection Connection { get; set; }

        internal Database.Engine.DBEngine DbEngine { get; set; }

        private ICollection<string> reservedWords = new List<string>();
        internal ICollection<string> ReservedWords
        {
            get { return reservedWords; }
            set { reservedWords = value; }
        }



        private static string SetUpServer(DbConnection connection)
        {
            string version = null;
            try
            {
                connection.Open();
                version = connection.ServerVersion;
                connection.Close();
                Log.LogInfo("Server Version: " + version);
            }
            catch (DbException dbe)
            {
                throw new TortException.PersistenceException("Failed to get server version", dbe);
            }
            return version;
        }

        private void Build(string connString, string provider)
        {
            switch (provider.ToUpperInvariant())
            {
                case "SYSTEM.DATA.SQLCLIENT":
                    Connection = new SqlConnection(connString);
                    if (SetUpServer(Connection).StartsWith("09", StringComparison.OrdinalIgnoreCase))
                    {
                        DbEngine = Engine.DBEngine.SqlServer2005;
                        ReservedWords = new ResourceListBuilder().
                            ConvertResourceToCollection(Tort.Properties.Resources.SQL2005ReservedWords);
                        return;
                    }
                    if (SetUpServer(Connection).StartsWith("08", StringComparison.OrdinalIgnoreCase))
                    {
                        DbEngine = Engine.DBEngine.SqlServer2000;
                        ReservedWords = new ResourceListBuilder().
                            ConvertResourceToCollection(Tort.Properties.Resources.SQL2000ReservedWords);
                        return;
                    }
                    break;
                case "SYSTEM.DATA.ORACLECLIENT":
                    Connection = new OracleConnection(connString);
                    if (SetUpServer(Connection).StartsWith("10", StringComparison.OrdinalIgnoreCase))
                    {
                        DbEngine = Engine.DBEngine.Oracle10g;
                        ReservedWords = new ResourceListBuilder().
                            ConvertResourceToCollection(Tort.Properties.Resources.Ora10gReservedWords);
                        return;
                    }
                    if (SetUpServer(Connection).StartsWith("9", StringComparison.OrdinalIgnoreCase))
                    {
                        DbEngine = Engine.DBEngine.Oracle9i;
                        ReservedWords = new ResourceListBuilder().
                            ConvertResourceToCollection(Tort.Properties.Resources.Ora9iReservedWords);
                        return;
                    }
                    break;
            }
        }

        internal static bool IgnoreUnicode
        {
            get { return ConfigurationManager.AppSettings.Get("IgnoreUnicode") == "True" ? true : false; }
        }
    }
}
